#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"



SLNode* SLInit(SLNode** head)
{
	assert(*head);
	assert(head);
	SLNode* tmp = (SLNode*)malloc(sizeof(SLNode));
	if (tmp == NULL)
	{
		assert("Init Malloc Fail");
		return NULL;
	}
	*head = tmp;
	(*head)->data = -1;
	(*head)->next = *head;
	(*head)->prev = *head;
	return *head;
}

void SLDestory(SLNode* head)
{
	SLNode* cur = head->next;
	while (cur != head && cur->next != head)
	{
		SLNode* del = cur;
		cur = cur->next;
		free(del);
	}
	free(head);
	head = NULL;
}

SLNode* BuySLNode(SLDataType x)
{
	SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));
	if (newnode == NULL)
	{
		assert("BuySLNode Malloc Fail");
		return NULL;
	}

	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	return newnode;
}

void SLPrint(SLNode* head)
{
	SLNode* cur = head->next;
	while (cur != head)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

void SLBackPush(SLNode* head, SLDataType x)
{
	assert(head);
	SLNode* newnode = BuySLNode(x);

	newnode->prev = head->prev;
	newnode->next = head;
	head->prev->next = newnode;
	head->prev = newnode;
}

void SLFrontPush(SLNode* head, SLDataType x)
{
	assert(head);
	SLNode* newnode = BuySLNode(x);
	
	head->next->prev = newnode;
	newnode->next = head->next;
	newnode->prev = head;
	head->next = newnode;
}

bool SLEmpty(SLNode* head)
{
	assert(head);
	if (head->next == head)
	{
		return true;
	}
	return false;
}

void SLBackPop(SLNode* head)
{
	assert(head);
	assert(!SLEmpty(head));
	if (!SLEmpty(head))
	{
		SLNode* del = head->prev;
		head->prev->prev->next = head;
		head->prev = head->prev->prev;
		free(del);
		del = NULL;
	}
}

void SLFrontPop(SLNode* head)
{
	assert(head);
	assert(!SLEmpty(head));
	SLNode* del = head->next;
	del->next->prev = head;
	head->next = del->next;
	free(del);
}

SLNode* SLDataFind(SLNode* head, SLDataType x)
{
	assert(head);
	assert(!SLEmpty(head));
	SLNode* cur = head->next;
	while (cur != head)
	{
		if (cur->data == x)
			return cur;
		cur = cur->next;
	}
	return NULL;
}

void SLInserAfter(SLNode* pos, SLDataType x)
{
	assert(pos);
	SLNode* newnode = BuySLNode(x);
    
	newnode->next = pos->next;
	newnode->prev = pos;
	pos->next->prev = newnode;
	pos->next = newnode;
}

void SLErase(SLNode* pos)
{
	assert(pos);
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	free(pos);
}


